/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.editor;

import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.EditorError;


/**
 * Editor simple error
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkSimpleError implements EditorError
{
	private final String absolutePath;
	private boolean consumed;
	private final Editor<?> editor;
	private final String message;
	private final Object value;
	private final Object userData;
	private int pathPrefixLength;

	DkSimpleError(final DkBeanEditorDelegate<?, ?> delegate, final String message, final Object value, final Object userData)
	{
		this.absolutePath = delegate.getPath();
		this.editor = delegate.getEditorWrapper();
		this.message = message;
		this.value = value == null ? delegate.getObject() : value;
		this.userData = userData;
	}

	/**
	 * Used to construct an error for an Editor that does not have a delegate.
	 */
	DkSimpleError(final DkBeanEditorDelegate<?, ?> delegate, final String message, final Object value, final Object userData,
			final String extraPath, final Editor<?> leafEditor)
	{
		assert extraPath != null && extraPath.length() > 0;
		this.absolutePath = delegate.getPath() + extraPath;
		this.editor = leafEditor;
		this.message = message;
		this.value = value;
		this.userData = userData;
	}

	@Override
	public String getAbsolutePath()
	{
		return absolutePath;
	}

	@Override
	public Editor<?> getEditor()
	{
		return editor;
	}

	@Override
	public String getMessage()
	{
		return message;
	}

	@Override
	public String getPath()
	{
		return absolutePath.substring(pathPrefixLength);
	}

	@Override
	public Object getUserData()
	{
		return userData;
	}

	@Override
	public Object getValue()
	{
		return value;
	}

	@Override
	public boolean isConsumed()
	{
		return consumed;
	}

	@Override
	public void setConsumed(final boolean consumed)
	{
		this.consumed = consumed;
	}

	/**
	 * For debugging use only.
	 */
	@Override
	public String toString()
	{
		return getMessage() + " @ " + getPath();
	}

	void setPathPrefixLength(final int pathPrefixLength)
	{
		this.pathPrefixLength = pathPrefixLength;
	}
}
